const express = require('express');
const router = express.Router();
const {Article}  = require('../../models')
const {Op} = require('sequelize')
/* GET home page. */
router.get('/', async function(req, res, next) {
    
    try {
        const query = req.query
        const currentPage = Math.abs(Number(query.currentPage)) || 1
        const pageSize = Math.abs(Number(query.pageSize)) || 10
        const offset = (currentPage - 1) * pageSize
        const condition = {
            order: [['id','desc']],
            limit: pageSize,
            offset
        }
       
        
        if(query.title) {
            condition.where = {
                title: {
                    [Op.like]: `%${query.title}%`
                }
            }
        }
        
     
        const {count, rows} = await Article.findAndCountAll(condition)
        res.json( {status: true, message: 'ok', data: {
            articles: rows,
            pagination: {
                totle: count,
                pageSize,
                currentPage
            }
        } });
    } catch (error) {
        res.status(500).json({
            status: false, 
            message: 'error',
            error: [error.message]
        })
    }
    
});
router.get('/:id', async function(req, res, next) {
    try {
        const {id} = req.params
        const article = await Article.findByPk(id)
        if(article)  res.json( {status: true, message: 'ok', data: article });
        else res.status(404).json({status: false, message: '文章没找到'})
    } catch (error) {
        res.status(500).json({
            status: false, 
            message: 'error',
            error: [error.message]
        })
    }
    
});
router.post('/', async function(req, res, next) {
    try {
        const body = {
            title: req.body.title,
            content: req.body.content
        }
        const article = await Article.create(body)
        
        res.status(201).json( {status: true, message: '创建文章成功', data: article });
    } catch (error) {
        res.status(500).json({
            status: false, 
            message: 'error',
            error: [error.message]
        })
    }
    
});
router.delete('/:id', async function(req, res, next) {
    try {
        const {id} = req.params
   
        const article = await Article.findByPk(id)
        
        
        if(article) {
            await article.destroy()
            res.json({status: true, message: '删除成功'})
        }
        else res.status(404).json({status: false, message: '文章不存在'})
    } catch (error) {
        res.status(500).json({
            status: false, 
            message: 'error',
            error: [error.message]
        })
    }
    
});
router.put('/:id', async function(req, res, next) {
    try {
        const {id} = req.params
        
        const body =  {
            title: req.body.title,
            content: req.body.content
        }
        const article = await Article.findByPk(id)
        
        
        if(article) {
            await article.update(body)
            res.json({status: true, message: '更新成功',data:article})
        }
        else res.status(404).json({status: false, message: '文章不存在'})
    } catch (error) {
        res.status(500).json({
            status: false, 
            message: 'error',
            error: [error.message]
        })
    }
    
});


module.exports = router;
